package application

import (
	"time"

	"github.com/jinzhu/gorm"
)

// CREATE TABLE `knowledges` (
//
//	`id` int(11) NOT NULL AUTO_INCREMENT,
//	`name` varchar(255) DEFAULT NULL, // 知识库名称
//	`description` varchar(255) DEFAULT NULL, // 知识库描述
//	`type` tinyint(1) NOT NULL DEFAULT 1,  知识库类型(1:默认类型)
//	`file_path` varchar(255) DEFAULT NULL, 文件路径
//	`created_at` datetime DEFAULT NULL, 创建时间
//	`updated_at` datetime DEFAULT NULL, 更新时间
//	PRIMARY KEY (`id`)
//
// ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
type Knowledge struct {
	Id          int       `json:"id" gorm:"column:id;primary_key"`
	Name        string    `json:"name" gorm:"column:name"`
	Description string    `json:"description" gorm:"column:description"`
	Type        int       `json:"type" gorm:"column:type"`
	FilePath    string    `json:"file_path" gorm:"column:file_path"`
	CreatedAt   time.Time `json:"created_at" gorm:"column:created_at"`
	UpdatedAt   time.Time `json:"updated_at" gorm:"column:updated_at"`
	KnowledgeId string    `json:"knowledge_id" gorm:"column:knowledge_id"`
}

// CREATE TABLE `app_knowledge_relate` (
//
//	`id` int(11) NOT NULL AUTO_INCREMENT, // 主键ID
//	`knowledge_id` int(11) DEFAULT NULL,  // 知识库ID
//	`app_id` int(11) DEFAULT NULL, // 应用ID
//	`created_at` datetime DEFAULT NULL,
//	`updated_at` datetime DEFAULT NULL,
//	PRIMARY KEY (`id`)
//
// ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
type AppKnowledgeRelate struct {
	Id          int       `json:"id" gorm:"column:id;primary_key"`
	KnowledgeId int       `json:"knowledge_id" gorm:"column:knowledge_id"`
	AppId       int       `json:"app_id" gorm:"column:app_id"`
	CreatedAt   time.Time `json:"created_at" gorm:"column:created_at"`
	UpdatedAt   time.Time `json:"updated_at" gorm:"column:updated_at"`
}

func (k *Knowledge) TableName() string {
	return "knowledges"
}
func (k *Knowledge) Create(db *gorm.DB) error {
	return db.Table(k.TableName()).Create(k).Error
}
func (k *Knowledge) GetList(db *gorm.DB) (knowledges []Knowledge, err error) {
	err = db.Table(k.TableName()).Find(&knowledges).Error
	return
}
func (k *Knowledge) GetById(db *gorm.DB) error {
	return db.Table(k.TableName()).Where("id = ?", k.Id).First(k).Error
}

func (a *AppKnowledgeRelate) TableName() string {
	return "app_knowledge_relate"
}
func (a *AppKnowledgeRelate) Create(db *gorm.DB) error {
	return db.Table(a.TableName()).Create(a).Error
}
func (a *AppKnowledgeRelate) GetList(db *gorm.DB) (appKnowledgeRelates []AppKnowledgeRelate, err error) {
	err = db.Table(a.TableName()).Find(&appKnowledgeRelates).Error
	return
}
func (a *AppKnowledgeRelate) GetListByAppId(db *gorm.DB) (appKnowledgeRelates []AppKnowledgeRelate, err error) {
	err = db.Table(a.TableName()).Where("app_id = ?", a.AppId).Find(&appKnowledgeRelates).Error
	return
}
